/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2015-9-22 17:35:18
 ******************************************************************************/
package cn.cmcc.online.smsapi.sdk.smsmessage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
import cn.cmcc.online.smsapi.sdk.util.JsonUtil;

public class BusinessSmsMessage implements java.io.Serializable, Cloneable {

    private static final long serialVersionUID = 1L;
    public static BusinessSmsMessage emptyObj = null;
    public HashMap<String, String> imagePathMap = null;
    public HashMap<String, Object> extendParamMap = null;
    // 所有内容解析出来的map，根据这个map判断类型
    public Map<String, Object> valueMap = null;
    public JSONObject bubbleJsonObj = null;// 汽泡视图数据对象
    public int simIndex = -1;// 双卡双带时需要使用
    public String simName = "";// 双卡的名字

    public String titleNo;// for test

    public String originatingAddress;

    public String messageBody;
    public boolean reBindData = false;// 重新绑定数据

    public long smsId = -1;
    public boolean isBgVis;

    public boolean isPopByWeishi = false;
    public byte viewType = 0;// 0:弹窗 1:汽泡

    // 表格所需要的数据
    HashMap<String, List<Map<String, String>>> tableMapData = null;
    HashMap<String, JSONArray> tableJsonData = null;

    // 增加时间
    public long msgTime = 0;

    // 信息类型：0为短信，1为彩信
    // public int msgType = 0;

    // 0:表示对方,1:表示自己
    // public int type = 0;

    public String getCenterAddress() {
        return "";
    }

    public long getSmsId() {
        return smsId;
    }

    public void setSmsId(long smsId) {
        this.smsId = smsId;
    }

    public String getOriginatingAddress() {

        return originatingAddress;
    }

    public void setOriginatingAddress(String originatingAddress) {
        this.originatingAddress = originatingAddress;
    }

    public String getMessageBody() {

        return messageBody;
    }

    public void setMessageBody(String messageBody) {
        this.messageBody = messageBody;
    }

    public String getTitleNo() {
        return titleNo;
    }

    public void setTitleNo(String titleNo) {
        this.titleNo = titleNo;
    }

    /**
     * 获取识别结果值
     * 
     * @param key
     * @return
     */
    public Object getValue(String key) {
        if (viewType == 0) {
            // 弹窗
            if (valueMap != null) {
                return valueMap.get(key);
            }
        }
        else {
            // 汽泡
            if (bubbleJsonObj != null && bubbleJsonObj.has(key)) {
                try {
                    Object object = bubbleJsonObj.get(key);
                    String tmp = null;
                    if (object!=null) {
                        tmp  = object.toString();
                        if (tmp.equalsIgnoreCase("null")) {
                            return null;
                        }
                    }
                    return object;
                }
                catch (Throwable e) {
                    // e.printStackTrace();
                    LogManager.e(Constant.TAG, "getValue: ", e);

                }
            }
        }
        return null;
    }

    /**
     * 获取识别结果值
     * 
     * @param key
     * @return
     */
    public void putValue(String key, Object value) {
        if (key == null || value == null) {
            return;
        }
        if (viewType == 0) {
            // 弹窗
            if (valueMap == null) {
                valueMap = new HashMap<String, Object>();
            }
            valueMap.put(key, value);
        } else {
            // 汽泡
            if (bubbleJsonObj == null) {
                bubbleJsonObj = new JSONObject();
            }
            try {
                bubbleJsonObj.put(key, value);
            } catch (Throwable e) {
//                e.printStackTrace();
                LogManager.e(Constant.TAG, "putValue: ", e);

            }
        }
        return;
    }

    /**
     * 获取扩展参数值
     * 
     * @param key
     * @return
     */
    public Object getExtendParamValue(String key) {
        if (extendParamMap != null) {
            return extendParamMap.get(key);
        }
        return null;
    }

    /**
     * 获取img图片名称
     * 
     * @param key
     * @return
     */
    public String getImgNameByKey(String key) {
        String imgName = null;
        if (viewType == 0) {
            // 弹窗
            if (imagePathMap != null) {
                imgName = imagePathMap.get(key);
            }
        } else {
            // 汽泡
            if (bubbleJsonObj != null && bubbleJsonObj.has(key)) {
                try {
                    imgName = (String) bubbleJsonObj.get(key);
                } catch (Throwable e) {
//                    e.printStackTrace();
                    LogManager.e(Constant.TAG, "getImgNameByKey: ", e);

                }
            }
        }
        /* SDK-410 huangzhiqiang 20160426 start */
        return imgName != null ? imgName.trim().replace(" ", "") : imgName;
        /* SDK-410 huangzhiqiang 20160426 end */
    }

    /***
     * 获取表格所需数据
     * 
     * @return
     */
    public int getTableDataSize(String key) {
        // "view_list_text";"view_list_text_not_line"
        int size = 0;
        try {
            if (viewType == 0) {
                // 弹窗
                if (tableMapData == null) {
                    tableMapData = new HashMap<String, List<Map<String, String>>>();
                }
                List<Map<String, String>> mapData = tableMapData.get(key);
                if (mapData == null) {
                    Object obj = getValue(key);
                    if (obj != null && obj instanceof List) {
                        mapData = (List<Map<String, String>>) obj;
                        tableMapData.put(key, mapData);
                    }
                }
                if (mapData != null) {
                    size = mapData.size();
                }

            } else {
                // 汽泡
                if (tableJsonData == null) {
                    tableJsonData = new HashMap<String, JSONArray>();
                }
                JSONArray jsData = tableJsonData.get(key);
                if (jsData == null) {
                    Object obj = getValue(key);
                    if (obj != null && obj instanceof JSONArray) {
                        jsData = (JSONArray) obj;
                        tableJsonData.put(key, jsData);
                    }
                }
                if (jsData != null) {
                    size = jsData.length();
                    /* SDK-412 huangzhiqiang 20160426 start */
//                    if (size > 5)
//                        size = 5; // 汽泡表格行数最多5行
                    /* SDK-412 huangzhiqiang 20160426 end */
                }
            }
        } catch (Throwable e) {
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "getTableDataSize: ", e);

        }
        return size;
    }

    /***
     * 获取表格所需数据
     * 
     * @return
     */
    public Object getTableData(int pos, String tableKey) {
        if (viewType == 0) {// 弹窗
            if (tableMapData != null) {
                List<Map<String, String>> list = tableMapData.get(tableKey);
                try {
                    if (list != null && pos >= 0 && list.size() > pos) {
                        return list.get(pos);
                    }
                } catch (Throwable e) {
//                    e.printStackTrace();
                    LogManager.e(Constant.TAG, "getTableData: ", e);

                }
                // return tableMapData.get(pos);
            }
        } else {// 汽泡
            if (tableJsonData != null) {
                try {
                    JSONArray arr = tableJsonData.get(tableKey);
                    if (arr != null && pos >= 0 && arr.length() > pos) {
                        return arr.get(pos);
                    }
                } catch (Throwable e) {
//                    e.printStackTrace();
                    LogManager.e(Constant.TAG, "getTableData: ", e);

                }
            }
        }
        return null;
    }

    public boolean isDataNull(String tableKey) {
        if (viewType == 0) {// 弹窗
            if (tableMapData != null) {
                List<Map<String, String>> list = tableMapData.get(tableKey);
                if (list != null && list.size() > 0) {
                    return false;
                }
            }
        } else {// 汽泡
            if (tableJsonData != null) {
                JSONArray arr = tableJsonData.get(tableKey);
                if (arr != null && arr.length() > 0) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }

    public static BusinessSmsMessage createMsgObj() {
        try {
            if (emptyObj == null) {
                emptyObj = new BusinessSmsMessage();
            }
            return (BusinessSmsMessage) emptyObj.clone();
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "createMsgObj: ", e);

        }
        return new BusinessSmsMessage();
    }

    public JSONArray getActionJsonArray() {
        Object objAction = getValue("ADACTION");
        if (objAction == null) {
            return null;
        }
        if (objAction instanceof JSONArray) {
            return (JSONArray) objAction;
        }
        String actionStr = (String) objAction;
        JSONArray actionArr = JsonUtil.parseStrToJsonArray(actionStr);
        putValue("ADACTION", actionArr);
        return actionArr;
    }

    // public void readExternal(ObjectInput input) throws IOException,
    // ClassNotFoundException {
    // // TODO Auto-generated method stub
    // try {
    // simIndex = input.readInt();
    // viewType = input.readByte();
    // smsId = input.readLong();
    // msgTime = input.readLong();
    //
    // reBindData = input.readBoolean();
    // isBgVis = input.readBoolean();
    // isPopByWeishi = input.readBoolean();
    //
    // simName = input.readUTF();
    // titleNo = input.readUTF();
    // originatingAddress = input.readUTF();
    // messageBody = input.readUTF();
    //
    // imagePathMap = (HashMap<String, String>) input.readObject();
    // extendParamMap = (HashMap<String, Object>) input.readObject();
    // valueMap = (HashMap<String, Object>) input.readObject();
    // tableMapData = (HashMap<String, List<Map<String, String>>>)
    // input.readObject();
    //
    // } catch (Throwable e) {
    // e.printStackTrace();
    // }
    // }
    //
    // public void writeExternal(ObjectOutput output) throws IOException {
    // try {
    // output.writeInt(simIndex);
    // output.writeByte(viewType);
    //
    // output.writeLong(smsId);
    // output.writeLong(msgTime);
    //
    // output.writeBoolean(reBindData);
    // output.writeBoolean(isBgVis);
    // output.writeBoolean(isPopByWeishi);
    //
    // output.writeUTF(simName);
    // output.writeUTF(titleNo);
    // output.writeUTF(originatingAddress);
    // output.writeUTF(messageBody);
    //
    // output.writeObject(imagePathMap);
    // output.writeObject(extendParamMap);
    // output.writeObject(valueMap);
    // output.writeObject(tableMapData);
    // } catch (Throwable e) {
    // e.printStackTrace();
    // }
    // }
}
